using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.SupplyChain.Purchase;
using HengTong.Model.Db.SupplyChain.Material;
using TiaoTaoHelper.Wtm.Models;


namespace HengTong.ViewModel.SupplyChain.SysPurchaseRequirementPoolEntryVms
{
    public partial class SysPurchaseRequirementPoolEntryListVm : BaseListVm<SysPurchaseRequirementPoolEntryView, SysPurchaseRequirementPoolEntrySearcher>
    {

        protected override IEnumerable<IGridColumn<SysPurchaseRequirementPoolEntryView>> InitGridHeader()
        {
            return new List<GridColumn<SysPurchaseRequirementPoolEntryView>>{
                this.MakeGridHeader(x => x.PoolName),
                this.MakeGridHeader(x => x.RequestBillName),
                this.MakeGridHeader(x => x.RequestBillEntryName),
                this.MakeGridHeader(x => x.MaterialName),
                this.MakeGridHeader(x => x.MaterialSkuName),
                this.MakeGridHeader(x => x.RequestQuantity),
                this.MakeGridHeader(x => x.EstimatedPrice),
                this.MakeGridHeader(x => x.BudgetAmount),
                this.MakeGridHeader(x => x.PurchasedQuantity),
                this.MakeGridHeader(x => x.RemainingQuantity),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.RequestBy),
                this.MakeGridHeader(x => x.PurchaseType),
                this.MakeGridHeader(x => x.MaterialType),
                this.MakeGridHeader(x => x.UrgencyLevel),
                this.MakeGridHeader(x => x.RequiredDeliveryDate),
                this.MakeGridHeader(x => x.ApprovalDate),
                this.MakeGridHeader(x => x.EntryDate),
                this.MakeGridHeader(x => x.PurchaseState),
                this.MakeGridHeader(x => x.PurchaseStrategy),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysPurchaseRequirementPoolEntryView> GetSearchQuery()
        {
            var query = DC.Set<SysPurchaseRequirementPoolEntry>()
                .CheckEqual(Searcher.PoolId, x=>x.PoolId)
                .CheckContain(Searcher.MaterialCode, x => x.Material.Code)
                .CheckContain(Searcher.MaterialName, x => x.Material.Name)
                .CheckContain(Searcher.MaterialSkuCode, x => x.MaterialSku.Code)
                .CheckContain(Searcher.MaterialSkuName, x => x.MaterialSku.Name)
                .CheckContain(Searcher.ErpName, x => x.MaterialSku.ErpName)
                .CheckEqual(Searcher.RequestBillCode, x => x.RequestBill.BillCode)
                .CheckContain(Searcher.ErpId, x => x.MaterialSku.ErpId)
                .CheckEqual(Searcher.RequestBillId, x=>x.RequestBillId)
                .CheckEqual(Searcher.MaterialId, x=>x.MaterialId)
                .CheckEqual(Searcher.MaterialSkuId, x=>x.MaterialSkuId)
                .CheckEqual(Searcher.GroupId, x=>x.GroupId)
                .CheckContain(Searcher.RequestBy, x=>x.RequestBy)
                .CheckEqual(Searcher.PurchaseType, x=>x.PurchaseType)
                .CheckEqual(Searcher.MaterialType, x=>x.MaterialType)
                .CheckBetween(Searcher.RequiredDeliveryDate?.GetStartTime(), Searcher.RequiredDeliveryDate?.GetEndTime(), x => x.RequiredDeliveryDate, includeMax: false)
                .CheckBetween(Searcher.EntryDate?.GetStartTime(), Searcher.EntryDate?.GetEndTime(), x => x.EntryDate, includeMax: false)
                .CheckEqual(Searcher.PurchaseState, x=>x.PurchaseState)
                .CheckEqual(Searcher.PurchaseStrategy, x=>x.PurchaseStrategy)
                .Select(x => new SysPurchaseRequirementPoolEntryView
                {
				    ID = x.ID,
                    PoolName = x.Pool.Name,
                    RequestBillName = x.RequestBill.BillCode,
                    RequestBillEntryName = x.RequestBillEntry.CreateBy,
                    MaterialName = x.Material.Code,
                    MaterialSkuName = x.MaterialSku.Code,
                    RequestQuantity = x.RequestQuantity,
                    EstimatedPrice = x.EstimatedPrice,
                    BudgetAmount = x.BudgetAmount,
                    PurchasedQuantity = x.PurchasedQuantity,
                    RemainingQuantity = x.RemainingQuantity,
                    GroupName = x.Group.FullName,
                    RequestBy = x.RequestBy,
                    PurchaseType = x.PurchaseType,
                    MaterialType = x.MaterialType,
                    UrgencyLevel = x.UrgencyLevel,
                    RequiredDeliveryDate = x.RequiredDeliveryDate,
                    ApprovalDate = x.ApprovalDate,
                    EntryDate = x.EntryDate,
                    PurchaseState = x.PurchaseState,
                    PurchaseStrategy = x.PurchaseStrategy,
                })
                .OrderBy(x => x.ID);
            return query;
        }
    }

    public class SysPurchaseRequirementPoolEntryView : SysPurchaseRequirementPoolEntry{
        [Display(Name = "需求池名称")]
        public String PoolName { get; set; }
        [Display(Name = "单据编号")]
        public String RequestBillName { get; set; }
        [Display(Name = "_Admin.CreateBy")]
        public String RequestBillEntryName { get; set; }
        [Display(Name = "编码")]
        public String MaterialName { get; set; }
        [Display(Name = "编码")]
        public String MaterialSkuName { get; set; }
        [Display(Name = "全称")]
        public String GroupName { get; set; }

    }
}
